export default function useAction(emits) {

  const circleIndex = ref(1);

  const circleTimer = ref(null);

  function circleInterval() {
    if (circleTimer.value) {
      clearInterval(circleTimer.value);
    } else {
      circleTimer.value = setInterval(() => {
        circleIndex.value++;
        if (circleIndex.value > 3) {
          circleIndex.value = 1;
        }
      }, 500);
    }
  }

  const refreshActive = ref(false);

  const startY = ref(0);

  const startRecord = ref(false);

  function refresherStart() {
    circleInterval();
    refreshActive.value = true;
    emits('refresher');

    setTimeout(() => {
      clearInterval(circleTimer.value);
      circleTimer.value = null;
      refreshActive.value = false;
    }, 1000);

  }


  function refresherTouchstart(e) {
    startY.value = e.touches[0].clientY;
    startRecord.value = true;
  }

  function refresherTouchend() {
    startRecord.value = false;
  }

  function refresherTouchmove(e) {
    if (!startRecord.value) {
      return;
    }

    let stopY = e.touches[0].clientY;

    let moveY = stopY - startY.value;

    if (moveY > 100) {
      startRecord.value = false;
      refresherStart();
    }
  }


  return {
    refreshActive,
    circleIndex,
    circleInterval,
    refresherTouchstart,
    refresherTouchmove,
    refresherTouchend
  }
}